package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.annotation.Id;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/dish")
@ApiOperation("菜品相关接口")
public class DishController {

    @Autowired
    DishService dishService;

    @PostMapping
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    @ApiOperation("新增菜品")
    public Result save(@RequestBody DishDTO dto){
        dishService.saveWithFlavor(dto);
        return Result.success();
    }
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> page(DishPageQueryDTO dto){
        PageResult result= dishService.pageQuery(dto);
        return  Result.success(result);

    }
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    @DeleteMapping
    @ApiOperation("批量删除菜品")
    public Result delete(@RequestParam List<Long> ids){

        dishService.deleteBatch(ids);
        return Result.success();
    }
    @GetMapping("/{id}")
    @ApiOperation("获取菜品信息")
    public Result getDish(@PathVariable Integer id){
      DishVO dishVO= dishService.getDish(id);
      return Result.success(dishVO);
    }
    @PutMapping
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    @ApiOperation("修改菜品信息")
    public Result updateDish(@RequestBody DishDTO dto){
        dishService.updateDish(dto);
        return Result.success();
    }
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result updateStatus(@PathVariable Integer status, Long id){
        dishService.updateStatus(id,status);
        return Result.success();
    }
    @GetMapping("/list")

    public Result<List<DishVO>> getCategory(Long categoryId){
        List<DishVO>dishVOS= dishService.getCategory(categoryId);
        return Result.success(dishVOS);
    }
}
